﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class BooleanVariable : Variable
    {
        private bool innerCurrentBoolValue;
        private bool innerDefaultBoolValue;

        public bool DefaultBoolValue
        {
            get
            {
                //bool retValue;
                //if (!bool.TryParse(base.DefaultValue, out retValue))
                //{
                //    throw new ArgumentException();
                //}
                return innerDefaultBoolValue;
            }
            set
            {
                innerDefaultBoolValue = value;
                base.DefaultValue = value.ToString();
            }
        }

        public override string CurrentValue
        {
            get { return base.CurrentValue; }
            set
            {
                if (currentValue != value)
                {
                    if (!bool.TryParse(value, out innerCurrentBoolValue))
                    {
                        throw new ArgumentException();
                    }
                    currentValue = value;
                    isChanged = true;
                }

            }
        }
        public bool CurrentBoolValue
        {
            get
            {
                //bool retValue;
                //if (!bool.TryParse(base.CurrentValue, out retValue))
                //{
                //    throw new ArgumentException();
                //}
                return innerCurrentBoolValue;
            }
            set
            {
                innerCurrentBoolValue = value;
                base.CurrentValue = value.ToString();
            }
        }

        public BooleanVariable(string name, string disc, string defValue,int ID)
            : base(name, disc, defValue,ID)
        {
            type = typeof(bool);
            if (!bool.TryParse(base.DefaultValue, out innerDefaultBoolValue))
            {
                throw new ArgumentException();
            }

        }

        public static BooleanVariable ConvertFromVariableInstance(Variable var)//for DB deserialyzation
        {
            if ((var.Type).Equals(typeof(bool)))
            {
                try
                {
                    BooleanVariable ret = (BooleanVariable)var;
                    bool tmp;
                    if (!bool.TryParse(var.CurrentValue, out tmp))
                    {
                        return null;
                    }
                    ret.CurrentBoolValue = tmp;

                    if (!bool.TryParse(var.DefaultValue, out tmp))
                    {
                        return null;
                    }
                    ret.DefaultBoolValue = tmp;
                    return ret;
                }
                catch (Exception e)
                {
                    return null;
                }

            }
            else
            {
                return null;
            }
        }

    }
}
